package wh.coach.pivot3.table;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author xhw
 * @date 2024/1/20
 */

@EqualsAndHashCode(of = {"rowsKeys", "columnsKeys"})
@AllArgsConstructor
@Getter
@Setter
public class GroupKey implements Comparable<GroupKey> {
    private List<String> rowsKeys;
    private List<String> columnsKeys;

    @Override
    public int compareTo(@NotNull final GroupKey other) {
        int result = compareKeys(this.rowsKeys, other.rowsKeys);
        if (result != 0) {
            return result;
        }
        return this.compareKeys(this.columnsKeys, other.columnsKeys);
    }

    //如果当前的目标大于other的目标，返回1，否则返回-1
    private int compareKeys(final List<String> keys, final List<String> otherKeys) {

        int minSize = Math.min(keys.size(), otherKeys.size());
        for (int i = 0; i < minSize; i++) {
            int result = keys.get(i).compareTo(otherKeys.get(i));
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }

    @Override
    public String toString() {
        return "GroupKeys{" +
                "rowsKeys=" + rowsKeys +
                ", columnsKeys=" + columnsKeys +
                '}';
    }

    public List<String> getRowsKey() {
        return rowsKeys;
    }

    public List<String> getColumnsKey() {
        return columnsKeys;
    }
}
